package com.chris.cc.user.service.impl;

import com.chris.cc.common.exception.InvalidArgumentException;
import com.chris.cc.common.util.EntityNumberGenerator;
import com.chris.cc.user.mapper.DeptMapper;
import com.chris.cc.user.model.dto.DeptDto;
import com.chris.cc.user.model.entity.Dept;
import com.chris.cc.user.service.DeptService;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * Copyright (c) 2018-2025, chris All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * <p>
 * Author: haoka (haokang207@126.com)
 * <p>
 * Date: 2018/8/24 9:36
 **/
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Dept add(DeptDto dept) throws InvalidArgumentException {
        Dept u = Dept.builder()
                .enable(dept.getEnable())
                .name(dept.getName())
                .contact(dept.getContact())
                .address(dept.getAddress())
                .deptNo(EntityNumberGenerator.generateDeptNo(null))
                .tel(dept.getTel())
                .build();
        u.setModifyDate(new Date());
        u.setCreateDate(new Date());
        try {
            deptMapper.insert(u);
        } catch (DuplicateKeyException e) {
            throw new InvalidArgumentException("公司名称已存在", dept.getName());
        }
        return u;
    }

    @Override
    public Page<Dept> query(Boolean enable, String keyword, Pageable page) {
        Example e = new Example(Dept.class);
        Example.Criteria criteria = e.createCriteria();
        if (enable != null) {
            criteria.andEqualTo("enable", enable);
        }
        if (StringUtils.isNotBlank(keyword)) {
            Example.Criteria or = e.createCriteria();
            or.orLike("name", "%" + keyword + "%");
            or.orLike("deptNo", "%" + keyword + "%");
            or.orLike("address", "%" + keyword + "%");
            or.orLike("contact", "%" + keyword + "%");
            or.orLike("tel", "%" + keyword + "%");
            e.and(or);
        }
        RowBounds rowBounds = new RowBounds(Long.valueOf(page.getOffset()).intValue(), page.getPageSize());
        List<Dept> list = deptMapper.selectByExampleAndRowBounds(e, rowBounds);
        int total = deptMapper.selectCountByExample(e);
        return new PageImpl<>(list, page, total);
    }

    @Override
    public Dept toggle(Long deptId, Boolean enable) {
        if (deptId == null || enable == null) {
            return null;
        }
        Dept u = Dept.builder()
                .deptId(deptId)
                .enable(enable)
                .build();
        u.setModifyDate(new Date());
        deptMapper.updateByPrimaryKeySelective(u);
        return deptMapper.selectByPrimaryKey(deptId);
    }

    @Override
    public Dept update(DeptDto dept) throws InvalidArgumentException {
        if (dept.getDeptId() == null) {
            return null;
        }
        Dept u = Dept.builder()
                .deptId(dept.getDeptId())
                .name(dept.getName())
                .contact(dept.getContact())
                .address(dept.getAddress())
                .tel(dept.getTel())
                .enable(dept.getEnable())
                .build();
        u.setModifyDate(new Date());
        try {
            deptMapper.updateByPrimaryKeySelective(u);
        } catch (DuplicateKeyException e) {
            throw new InvalidArgumentException("手机号码已存在", dept.getName());
        }
        return deptMapper.selectByPrimaryKey(dept.getDeptId());
    }

    @Override
    public Dept get(Long deptId) {
        if (deptId == null) {
            return null;
        }
        return deptMapper.selectByPrimaryKey(deptId);
    }
}
